export default{
	data(){
		return {
			vi_pageSize:"",
			vi_pageTitle:"",
			vi_router:"",
			vi_gobackTimer:null,	//后退计时器，一般在数据操作完成后定时返回，开始定时后会与手动返回冲突，在onUnload额外处理
			vi_loading:false,		//加载动画启动
			vi_loadingMask:false,	//加载动画使用遮罩，一次性
			host_image:this.$host.image,
			autoTurnPage:false,
			pageObj:{
				currentPage:1,//请求页码
				pageSize:20,//一页记录数
				listLen:0,//当前页记录数
				total:0,//总记录数
				listStart:"list",
				totalStart:"total",
			},
			topfix:"",		//基础值加上状态栏高度，单位px。计算在App.vue
			shiftTop:{},	//用于弹出窗顶部偏移
			openList:[],	//openWin打开列表，用于清理
		}
	},
	computed: {
		//当前列表有下一页
		hasNextPage(){
			let {currentPage, pageSize, listLen, total} = this.pageObj;
			if(!this.autoTurnPage || listLen<1 || total<1){
				return false
			}
			if(listLen + (currentPage-2)*pageSize < total){
				return true
			}
			return false
		},
		caches: function(){
			return {
				user: this.$store.state.user,//用户信息
				dic: this.$store.state.dic.data,//字典
			}
		}
	},
	methods:{
		_getPageSize(){
			this["vi_pageSize"] = "pageSize-" + this.$sysInfo.pageSize
		},
		//
		//注册常用功能，给template数据渲染使用
		//
		//使用关键词链接到页面
		linkToKey(key, urlParams, linkType){
			if(!key){
				return
			}
			let obj = this.$getRouter(key);
			let {url, type, params={}} = obj;
			Object.assign(params, urlParams);
			if(Object.keys(params).length){
				url += "?"+ this.$api.stringParams(params);
			}
			if(urlParams&&typeof(urlParams)=="string"){
				type = urlParams
			}
			if(linkType){
				type = linkType;
			}
			// console.log(url);
			this.$api.linkTo(url, type)
		},
		//使用url链接到页面
		linkTo(url, type){
			this.$api.linkTo(url, type)
		},
		//格式化日期
		dateFormat(value, format){
			if(!value){
				return ""
			}
			return this.$api.dateFormat(value, {format})
		},
		//显示文本提示
		showTips(words, limit, options){
			this.$api.tips(words, limit, options);
		},
		//打开组件，微信小程序不能在template写对象格式的参数，仅可用变量传递对象
		openWin(name, ...params){
			let handle = this.$refs[name];
			if(handle&&handle.open){
				handle.open(...params);
				let openList = this.openList;
				openList.push(name);
				this.openList = [...new Set(openList)];//去重
			}else{
				console.log(`组件${name}不存在`, this.$refs);
			}
		},
		//关闭组件
		closeWin(name){
			let handle = this.$refs[name];
			if(handle&&handle.close){
				handle.close();
				let openList = this.openList;
				let idx = openList.indexOf(name);
				if(idx!=-1){
					openList.splice(idx,1)
				}
			}else{
				console.log(`组件${name}不存在`, this.$refs);
			}
		},
		//关闭已打开组件，清空openList。except排除列表
		clearWin(except=[]){
			let openList = this.openList;
			if(this.$api.isString(except)){
				except = [except]
			}
			for(let name of openList){
				if(except.indexOf(name)!=-1){
					continue
				}
				let handle = this.$refs[name];
				if(handle&&handle.close){
					handle.close();
				}
			}
			openList.splice(0);
		},
		//自动翻页功能，设置新页数据，刷新页码
		setPageData(res, resHandle, pageObj){
			let {currentPage, listLen, listStart, totalStart} = pageObj;
			let list = this.$api.objPathGet(res, listStart),
				total = this.$api.objPathGet(res, totalStart);
			if(!list){
				console.error("翻页数据错误，注意检查", list);
				list =[]
			}
			//设置数据
			if(currentPage==1){
				resHandle.splice(0,resHandle.length);
			}
			resHandle.push(...list)
			Object.assign(pageObj, {
				currentPage: currentPage+1,
				listLen: list.length,
				total
			});
		},
		//自动翻页功能，重置页码
		resetPage(){
			Object.assign(this.pageObj,{
				currentPage:1,
				listLen:0,
				total:0,
			})
		},
	},
	//页面渲染前
	onLoad(){
		//获取页面尺寸，监听改变
		this._getPageSize();
		uni.onWindowResize(() => {//监听窗口重置
			this._getPageSize()
		});
		//刷新用户数据
		this.$store.dispatch("user/refresh");
		
		//获取页面标题，从自定义路由获取
		let pages = getCurrentPages(),
			currPage = pages[pages.length-1],
			currRoute = currPage.route;
		this["vi_router"] = this.$getRouter({key:"url", value:currRoute, match:"includes"});
		this["vi_pageTitle"] = this.vi_router? this.vi_router.name :"";
	},
	//创建后，除了页面、组件，还有其他未知东西
	created(){
		if(this.$sysInfo){
			this.topfix = this.$sysInfo.topfix;
			this.shiftTop = {top:this.topfix};
		}
	},
	//页面卸载前
	onUnload(){
		uni.offWindowResize(()=>{});//销毁监听
		//销毁返回定时器，不管是执行了定时返回还是手动返回，还是跳转到其他页面
		if(this.vi_gobackTimer){console.log("清除返回定时器");
			clearTimeout(this.vi_gobackTimer);
			this.vi_gobackTimer =null;
		}
	},
	//组件渲染前
	beforeMount(){
		
	},
	//组件销毁前
	beforeDestroy(){
		
	},
	//到底翻页
	onReachBottom(){
		if(this.autoTurnPage){
			if(this.hasNextPage){
				this.getData()
			}else{
				this.$api.msg("没有更多了")
			}
		}
	}
}